
use "$path\Intermediary Data\localshock_dataforReg_v2.dta",clear


drop if prix_ht==.
drop  j j3
replace d_dieselr=resid_movav_diesel
xtset id daten
gen d_dieselrott=ln(diesel_rotterdam_euro)-ln(l1.diesel_rotterdam_euro)
drop diesel_rotterdam_euro resid_movav_diesel dprix1 

drop d_dieselr
gen d_dieselr=d_dieselrott

merge m:m id using  "$path\Intermediary Data\base_id_suff_stat_6Y"
keep if _m==3
drop _m

merge m:m id using  "$path\Intermediary Data\base_base_suff_stat_6Y_forinteractkurtfreq"
keep if _m==3
drop _m


bysort id (daten): egen mean_prix=mean(prix)
*** Mean prices
capture drop price_lev


gen price_lev=(mean_prix>1215.414)
replace price_lev=. if mean_prix==.


***** CONSTRAINED 

capture drop ratio
gen ratio=kurtosis/freq

gen d_dieselr_lessmean=.
replace d_dieselr_lessmean=d_dieselr if price_lev==0
replace d_dieselr_lessmean=0 if price_lev==1

gen d_dieselr_meantomax=.
replace d_dieselr_meantomax=d_dieselr if price_lev==1
replace d_dieselr_meantomax=0 if price_lev==0


drop num_obs
gen num_obs=_n-1

capture drop local_shock5 local_shock15 local_shock4 local_shock6 local_shock7 local_shock8
capture drop coeff1_reg
capture drop coeff2_reg
capture drop coeff3_reg
capture drop coeff4_reg
capture drop coeff5_reg
capture drop coeff6_reg
capture drop coeff7_reg
capture drop coeff8_reg
capture drop coeff9_reg
capture drop coeff10_reg



capture drop coeff_c_reg

capture drop se_coeff1_reg
capture drop se_coeff2_reg
capture drop se_coeff3_reg
capture drop se_coeff4_reg
capture drop se_coeff5_reg
capture drop se_coeff6_reg
capture drop se_coeff7_reg
capture drop se_coeff8_reg
capture drop se_coeff9_reg
capture drop se_coeff10_reg

capture drop df_reg
capture drop r2
capture drop r2_a


gen coeff1_reg=.
gen coeff2_reg=.
gen coeff3_reg=.
gen coeff4_reg=.
gen coeff5_reg=.
gen coeff6_reg=.
gen coeff7_reg=.
gen coeff8_reg=.
gen coeff9_reg=.
gen coeff10_reg=.
gen coeff_c_reg=.

gen se_coeff1_reg=.
gen se_coeff2_reg=.
gen se_coeff3_reg=.
gen se_coeff4_reg=.
gen se_coeff5_reg=.
gen se_coeff6_reg=.
gen se_coeff7_reg=.
gen se_coeff8_reg=.
gen se_coeff9_reg=.
gen se_coeff10_reg=.
gen df_reg=.

gen r2=.
gen r2_a=.



****** H=30 ****

capture drop f_prix_ht 
capture drop f_prix_ht_temp
gen f_prix_ht=0

foreach i of numlist 1/29 {
 
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'
}


foreach i of numlist 30 {
 
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht


areg f_prix_ht c.d_dieselr_lessmean##(c.ratio ) c.d_dieselr_meantomax##(c.ratio )   ///
c.local_shock10##(c.ratio ) /// 
c.m_dprix##(c.ratio ) ///
 c.l.d_dieselr_lessmean##(c.ratio ) ///
 c.l2.d_dieselr_lessmean##(c.ratio ) ///
 c.l3.d_dieselr_lessmean##(c.ratio ) ///
 c.l4.d_dieselr_lessmean##(c.ratio ) /// 
 c.l5.d_dieselr_lessmean##(c.ratio ) ///
 c.l.d_dieselr_meantomax##(c.ratio ) ///
 c.l2.d_dieselr_meantomax##(c.ratio ) ///
 c.l3.d_dieselr_meantomax##(c.ratio ) ///
 c.l4.d_dieselr_meantomax##(c.ratio ) /// 
 c.l5.d_dieselr_meantomax##(c.ratio ) ///
		c.l.local_shock10##(c.ratio )  /// 
		c.l2.local_shock10##(c.ratio ) /// 
		c.l3.local_shock10##(c.ratio ) ///
		c.l4.local_shock10##(c.ratio ) /// 
		c.l5.local_shock10##(c.ratio ) ///
	c.l.m_dprix##(c.ratio )  /// 
	c.l2.m_dprix##(c.ratio ) /// 
	c.l3.m_dprix##(c.ratio ) /// 
	c.l4.m_dprix##(c.ratio ) ///
	c.l5.m_dprix##(c.ratio ), absorb(id) vce(cluster daten)

	
	replace r2=e(r2)
replace r2_a=e(r2_a)
	
matrix test=r(table)
matrix test=r(table)
replace coeff1_reg=_b[d_dieselr_lessmean] if num_obs==`i'
replace coeff2_reg=_b[d_dieselr_meantomax] if num_obs==`i'
replace coeff3_reg=_b[c.d_dieselr_lessmean#c.ratio] if num_obs==`i'
replace coeff4_reg=_b[c.d_dieselr_meantomax#c.ratio] if num_obs==`i'



replace coeff_c_reg=_b[_cons] if num_obs==`i'


replace se_coeff1_reg=_se[d_dieselr_lessmean] if num_obs==`i'
replace se_coeff2_reg=_se[d_dieselr_meantomax] if num_obs==`i'
replace se_coeff3_reg=_se[c.d_dieselr_lessmean#c.ratio] if num_obs==`i'
replace se_coeff4_reg=_se[c.d_dieselr_meantomax#c.ratio] if num_obs==`i'

replace df_reg=e(df_r) if num_obs==`i'
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'

preserve
keep coeff* r2 r2_a  se_coeff* df_reg num_obs
gen name="`i'"
gen charact="constrained"
keep if num_obs==30
save "$path\Graphs_Tables\Table5_constrained_PanelA_pricelev.dta", replace
restore 

}




***** UNCONSTRAINED ****



bysort id: gen first_obs=1 if _n==1

egen mean_kur_temp=mean(kur) if first_obs==1
egen mean_freq_temp=mean(freq) if first_obs==1
egen mean_kur=mean(mean_kur_temp)
egen mean_freq=mean(mean_freq_temp)
drop mean_kur_temp mean_freq_temp first_obs

gen kur_demean=kur/mean_kur
gen freq_demean=freq/mean_freq


drop num_obs
gen num_obs=_n-1

capture drop local_shock5 local_shock15 local_shock4 local_shock6 local_shock7 local_shock8
capture drop coeff1_reg
capture drop coeff2_reg
capture drop coeff3_reg
capture drop coeff4_reg
capture drop coeff5_reg
capture drop coeff6_reg
capture drop coeff7_reg
capture drop coeff8_reg
capture drop coeff9_reg
capture drop coeff10_reg
capture drop coeff11_reg
capture drop coeff12_reg



capture drop coeff_c_reg

capture drop se_coeff1_reg
capture drop se_coeff2_reg
capture drop se_coeff3_reg
capture drop se_coeff4_reg
capture drop se_coeff5_reg
capture drop se_coeff6_reg
capture drop se_coeff7_reg
capture drop se_coeff8_reg
capture drop se_coeff9_reg
capture drop se_coeff10_reg
capture drop se_coeff11_reg
capture drop se_coeff12_reg

capture drop df_reg
capture drop r2
capture drop r2_a


gen coeff1_reg=.
gen coeff2_reg=.
gen coeff3_reg=.
gen coeff4_reg=.
gen coeff5_reg=.
gen coeff6_reg=.
gen coeff7_reg=.
gen coeff8_reg=.
gen coeff9_reg=.
gen coeff10_reg=.
gen coeff11_reg=.
gen coeff12_reg=.
gen coeff_c_reg=.


gen se_coeff1_reg=.
gen se_coeff2_reg=.
gen se_coeff3_reg=.
gen se_coeff4_reg=.
gen se_coeff5_reg=.
gen se_coeff6_reg=.
gen se_coeff7_reg=.
gen se_coeff8_reg=.
gen se_coeff9_reg=.
gen se_coeff10_reg=.
gen se_coeff11_reg=.
gen se_coeff12_reg=.

gen df_reg=.
gen r2=.
gen r2_a=.

capture drop f_prix_ht 
capture drop f_prix_ht_temp
gen f_prix_ht=0




**** H=30 ***


drop num_obs
gen num_obs=_n-1

capture drop local_shock5 local_shock15 local_shock4 local_shock6 local_shock7 local_shock8
capture drop coeff1_reg
capture drop coeff2_reg
capture drop coeff3_reg
capture drop coeff4_reg
capture drop coeff5_reg
capture drop coeff6_reg
capture drop coeff7_reg
capture drop coeff8_reg
capture drop coeff9_reg
capture drop coeff10_reg
capture drop coeff11_reg
capture drop coeff12_reg



capture drop coeff_c_reg


capture drop se_coeff1_reg
capture drop se_coeff2_reg
capture drop se_coeff3_reg
capture drop se_coeff4_reg
capture drop se_coeff5_reg
capture drop se_coeff6_reg
capture drop se_coeff7_reg
capture drop se_coeff8_reg
capture drop se_coeff9_reg
capture drop se_coeff10_reg
capture drop se_coeff11_reg
capture drop se_coeff12_reg

capture drop df_reg
capture drop r2
capture drop r2_a


gen coeff1_reg=.
gen coeff2_reg=.
gen coeff3_reg=.
gen coeff4_reg=.
gen coeff5_reg=.
gen coeff6_reg=.
gen coeff7_reg=.
gen coeff8_reg=.
gen coeff9_reg=.
gen coeff10_reg=.
gen coeff11_reg=.
gen coeff12_reg=.
gen coeff_c_reg=.


gen se_coeff1_reg=.
gen se_coeff2_reg=.
gen se_coeff3_reg=.
gen se_coeff4_reg=.
gen se_coeff5_reg=.
gen se_coeff6_reg=.
gen se_coeff7_reg=.
gen se_coeff8_reg=.
gen se_coeff9_reg=.
gen se_coeff10_reg=.
gen se_coeff11_reg=.
gen se_coeff12_reg=.

gen df_reg=.
gen r2=.
gen r2_a=.

capture drop f_prix_ht 
capture drop f_prix_ht_temp
gen f_prix_ht=0


foreach i of numlist 0/29 {
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'
}

foreach i of numlist 30 {
 
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht



areg f_prix_ht c.d_dieselr_lessmean##(c.kur_demean c.freq_demean ) c.d_dieselr_meantomax##(c.kur_demean c.freq_demean )  ///
c.local_shock10##(c.kur_demean c.freq_demean) /// 
c.m_dprix##(c.kur_demean c.freq_demean ) ///
c.l.d_dieselr_lessmean##(c.kur_demean c.freq_demean ) ///
c.l2.d_dieselr_lessmean##(c.kur_demean c.freq_demean ) ///
c.l3.d_dieselr_lessmean##(c.kur_demean c.freq_demean ) ///
c.l4.d_dieselr_lessmean##(c.kur_demean c.freq_demean ) /// 
c.l5.d_dieselr_lessmean##(c.kur_demean c.freq_demean ) ///
c.l.d_dieselr_meantomax##(c.kur_demean c.freq_demean ) ///
c.l2.d_dieselr_meantomax##(c.kur_demean c.freq_demean ) ///
c.l3.d_dieselr_meantomax##(c.kur_demean c.freq_demean ) ///
c.l4.d_dieselr_meantomax##(c.kur_demean c.freq_demean ) /// 
c.l5.d_dieselr_meantomax##(c.kur_demean c.freq_demean ) ///
c.l.local_shock10##(c.kur_demean c.freq_demean )  /// 
c.l2.local_shock10##(c.kur_demean c.freq_demean ) /// 
c.l3.local_shock10##(c.kur_demean c.freq_demean ) ///
c.l4.local_shock10##(c.kur_demean c.freq_demean ) /// 
c.l5.local_shock10##(c.kur_demean c.freq_demean ) ///
c.l.m_dprix##(c.kur_demean c.freq_demean )  /// 
c.l2.m_dprix##(c.kur_demean c.freq_demean ) /// 
c.l3.m_dprix##(c.kur_demean c.freq_demean ) /// 
c.l4.m_dprix##(c.kur_demean c.freq_demean ) ///
c.l5.m_dprix##(c.kur_demean c.freq_demean ), absorb(id) vce(cluster daten)
	
replace r2=e(r2)
replace r2_a=e(r2_a)
	
matrix test=r(table)
replace coeff1_reg=_b[d_dieselr_lessmean] if num_obs==`i'
replace coeff2_reg=_b[d_dieselr_meantomax] if num_obs==`i'
replace coeff3_reg=_b[c.d_dieselr_lessmean#c.freq_demean] if num_obs==`i'
replace coeff4_reg=_b[c.d_dieselr_lessmean#c.kur_demean] if num_obs==`i'
replace coeff5_reg=_b[c.d_dieselr_meantomax#c.freq_demean] if num_obs==`i'
replace coeff6_reg=_b[c.d_dieselr_meantomax#c.kur_demean] if num_obs==`i'



replace coeff_c_reg=_b[_cons] if num_obs==`i'

replace se_coeff1_reg=_se[d_dieselr_lessmean] if num_obs==`i'
replace se_coeff2_reg=_se[d_dieselr_meantomax] if num_obs==`i'
replace se_coeff3_reg=_se[c.d_dieselr_lessmean#c.freq_demean] if num_obs==`i'
replace se_coeff4_reg=_se[c.d_dieselr_lessmean#c.kur_demean] if num_obs==`i'
replace se_coeff5_reg=_se[c.d_dieselr_meantomax#c.freq_demean] if num_obs==`i'
replace se_coeff6_reg=_se[c.d_dieselr_meantomax#c.kur_demean] if num_obs==`i'




replace df_reg=e(df_r) if num_obs==`i'
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'

preserve
keep coeff* r2 r2_a  se_coeff* df_reg num_obs
gen name="`i'"
gen charact="unconstrained"
keep if num_obs==30
save "$path\Graphs_Tables\Table5_unconstrained_PanelA_pricelev.dta", replace
restore 
}



***********************************************
***** PANEL B: Supermarkets

*************************************************************************





use "$path\Intermediary Data\localshock_dataforReg_v2.dta",clear




drop if prix_ht==.
drop j j3
replace d_dieselr=resid_movav_diesel
xtset id daten
gen d_dieselrott=ln(diesel_rotterdam_euro)-ln(l1.diesel_rotterdam_euro)
drop diesel_rotterdam_euro resid_movav_diesel dprix1 

drop d_dieselr
gen d_dieselr=d_dieselrott

merge m:1 id_pdv using "$path\Intermediary Data\data_PDV_autoroutes_supermarches_v3_withlonglat_mergecodeinsee.dta"
keep if _merge!=2 
drop _merge

merge m:m id using  "$path\Intermediary Data\base_id_suff_stat_6Y"
keep if _m==3
drop _m

merge m:m id using  "$path\Intermediary Data\base_base_suff_stat_6Y_forinteractkurtfreq"
keep if _m==3
drop _m

gen supermarche=0 if classif_num!=. 
replace supermarche=1 if (classif_num==1 | classif_num==2)



***** PLACEBO***
***** CONSTRAINED 

capture drop ratio
gen ratio=kurtosis/freq

capture drop d_dieselr_supermarche
capture drop d_dieselr_nosupermarche

gen d_dieselr_supermarche=.
replace d_dieselr_supermarche=d_dieselr if supermarche==1
replace d_dieselr_supermarche=0 if supermarche==0

gen d_dieselr_nosupermarche=.
replace d_dieselr_nosupermarche=d_dieselr if supermarche==0
replace d_dieselr_nosupermarche=0 if supermarche==1

summ d_dieselr, detail 


drop num_obs
gen num_obs=_n-1

capture drop local_shock5 local_shock15 local_shock4 local_shock6 local_shock7 local_shock8
capture drop coeff1_reg
capture drop coeff2_reg
capture drop coeff3_reg
capture drop coeff4_reg
capture drop coeff5_reg
capture drop coeff6_reg
capture drop coeff7_reg
capture drop coeff8_reg
capture drop coeff9_reg
capture drop coeff10_reg



capture drop coeff_c_reg

capture drop se_coeff1_reg
capture drop se_coeff2_reg
capture drop se_coeff3_reg
capture drop se_coeff4_reg
capture drop se_coeff5_reg
capture drop se_coeff6_reg
capture drop se_coeff7_reg
capture drop se_coeff8_reg
capture drop se_coeff9_reg
capture drop se_coeff10_reg

capture drop df_reg
capture drop r2
capture drop r2_a


gen coeff1_reg=.
gen coeff2_reg=.
gen coeff3_reg=.
gen coeff4_reg=.
gen coeff5_reg=.
gen coeff6_reg=.
gen coeff7_reg=.
gen coeff8_reg=.
gen coeff9_reg=.
gen coeff10_reg=.
gen coeff_c_reg=.

gen se_coeff1_reg=.
gen se_coeff2_reg=.
gen se_coeff3_reg=.
gen se_coeff4_reg=.
gen se_coeff5_reg=.
gen se_coeff6_reg=.
gen se_coeff7_reg=.
gen se_coeff8_reg=.
gen se_coeff9_reg=.
gen se_coeff10_reg=.
gen df_reg=.

gen r2=.
gen r2_a=.



****** H=30 ****

capture drop f_prix_ht 
capture drop f_prix_ht_temp
gen f_prix_ht=0

foreach i of numlist 1/29 {
 
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'
}


foreach i of numlist 30 {
 
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht


areg f_prix_ht c.d_dieselr_supermarche##(c.ratio ) c.d_dieselr_nosupermarche##(c.ratio )  ///
c.local_shock10##(c.ratio ) /// 
c.m_dprix##(c.ratio ) ///
 c.l.d_dieselr_supermarche##(c.ratio ) ///
 c.l2.d_dieselr_supermarche##(c.ratio ) ///
 c.l3.d_dieselr_supermarche##(c.ratio ) ///
 c.l4.d_dieselr_supermarche##(c.ratio ) /// 
 c.l5.d_dieselr_supermarche##(c.ratio ) ///
 c.l.d_dieselr_nosupermarche##(c.ratio ) ///
 c.l2.d_dieselr_nosupermarche##(c.ratio ) ///
 c.l3.d_dieselr_nosupermarche##(c.ratio ) ///
 c.l4.d_dieselr_nosupermarche##(c.ratio ) /// 
 c.l5.d_dieselr_nosupermarche##(c.ratio ) ///
		c.l.local_shock10##(c.ratio )  /// 
		c.l2.local_shock10##(c.ratio ) /// 
		c.l3.local_shock10##(c.ratio ) ///
		c.l4.local_shock10##(c.ratio ) /// 
		c.l5.local_shock10##(c.ratio ) ///
	c.l.m_dprix##(c.ratio )  /// 
	c.l2.m_dprix##(c.ratio ) /// 
	c.l3.m_dprix##(c.ratio ) /// 
	c.l4.m_dprix##(c.ratio ) ///
	c.l5.m_dprix##(c.ratio ), absorb(id) vce(cluster daten)

	
	replace r2=e(r2)
replace r2_a=e(r2_a)
	
matrix test=r(table)
matrix test=r(table)
replace coeff1_reg=_b[d_dieselr_supermarche] if num_obs==`i'
replace coeff2_reg=_b[d_dieselr_nosupermarche] if num_obs==`i'
replace coeff3_reg=_b[c.d_dieselr_supermarche#c.ratio] if num_obs==`i'
replace coeff4_reg=_b[c.d_dieselr_nosupermarche#c.ratio] if num_obs==`i'

replace coeff_c_reg=_b[_cons] if num_obs==`i'



replace se_coeff1_reg=_se[d_dieselr_supermarche] if num_obs==`i'
replace se_coeff2_reg=_se[d_dieselr_nosupermarche] if num_obs==`i'
replace se_coeff3_reg=_se[c.d_dieselr_supermarche#c.ratio] if num_obs==`i'
replace se_coeff4_reg=_se[c.d_dieselr_nosupermarche#c.ratio] if num_obs==`i'

replace df_reg=e(df_r) if num_obs==`i'
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'

preserve
keep coeff* r2 r2_a  se_coeff* df_reg num_obs
gen name="`i'"
gen charact="constrained"
keep if num_obs==30
save "$path\Graphs_Tables\Table5_constrained_PanelB_supermarkets", replace
restore 

}



***** UNCONSTRAINED ****



bysort id: gen first_obs=1 if _n==1

egen mean_kur_temp=mean(kur) if first_obs==1
egen mean_freq_temp=mean(freq) if first_obs==1
egen mean_kur=mean(mean_kur_temp)
egen mean_freq=mean(mean_freq_temp)
drop mean_kur_temp mean_freq_temp first_obs

gen kur_demean=kur/mean_kur
gen freq_demean=freq/mean_freq


drop num_obs
gen num_obs=_n-1

capture drop local_shock5 local_shock15 local_shock4 local_shock6 local_shock7 local_shock8
capture drop coeff1_reg
capture drop coeff2_reg
capture drop coeff3_reg
capture drop coeff4_reg
capture drop coeff5_reg
capture drop coeff6_reg
capture drop coeff7_reg
capture drop coeff8_reg
capture drop coeff9_reg
capture drop coeff10_reg
capture drop coeff11_reg
capture drop coeff12_reg



capture drop coeff_c_reg


capture drop se_coeff1_reg
capture drop se_coeff2_reg
capture drop se_coeff3_reg
capture drop se_coeff4_reg
capture drop se_coeff5_reg
capture drop se_coeff6_reg
capture drop se_coeff7_reg
capture drop se_coeff8_reg
capture drop se_coeff9_reg
capture drop se_coeff10_reg
capture drop se_coeff11_reg
capture drop se_coeff12_reg

capture drop df_reg
capture drop r2
capture drop r2_a


gen coeff1_reg=.
gen coeff2_reg=.
gen coeff3_reg=.
gen coeff4_reg=.
gen coeff5_reg=.
gen coeff6_reg=.
gen coeff7_reg=.
gen coeff8_reg=.
gen coeff9_reg=.
gen coeff10_reg=.
gen coeff11_reg=.
gen coeff12_reg=.
gen coeff_c_reg=.


gen se_coeff1_reg=.
gen se_coeff2_reg=.
gen se_coeff3_reg=.
gen se_coeff4_reg=.
gen se_coeff5_reg=.
gen se_coeff6_reg=.
gen se_coeff7_reg=.
gen se_coeff8_reg=.
gen se_coeff9_reg=.
gen se_coeff10_reg=.
gen se_coeff11_reg=.
gen se_coeff12_reg=.

gen df_reg=.
gen r2=.
gen r2_a=.

capture drop f_prix_ht 
capture drop f_prix_ht_temp
gen f_prix_ht=0





**** H=30 ***


drop num_obs
gen num_obs=_n-1

capture drop local_shock5 local_shock15 local_shock4 local_shock6 local_shock7 local_shock8
capture drop coeff1_reg
capture drop coeff2_reg
capture drop coeff3_reg
capture drop coeff4_reg
capture drop coeff5_reg
capture drop coeff6_reg
capture drop coeff7_reg
capture drop coeff8_reg
capture drop coeff9_reg
capture drop coeff10_reg
capture drop coeff11_reg
capture drop coeff12_reg



capture drop coeff_c_reg


capture drop se_coeff1_reg
capture drop se_coeff2_reg
capture drop se_coeff3_reg
capture drop se_coeff4_reg
capture drop se_coeff5_reg
capture drop se_coeff6_reg
capture drop se_coeff7_reg
capture drop se_coeff8_reg
capture drop se_coeff9_reg
capture drop se_coeff10_reg
capture drop se_coeff11_reg
capture drop se_coeff12_reg

capture drop df_reg
capture drop r2
capture drop r2_a


gen coeff1_reg=.
gen coeff2_reg=.
gen coeff3_reg=.
gen coeff4_reg=.
gen coeff5_reg=.
gen coeff6_reg=.
gen coeff7_reg=.
gen coeff8_reg=.
gen coeff9_reg=.
gen coeff10_reg=.
gen coeff11_reg=.
gen coeff12_reg=.
gen coeff_c_reg=.


gen se_coeff1_reg=.
gen se_coeff2_reg=.
gen se_coeff3_reg=.
gen se_coeff4_reg=.
gen se_coeff5_reg=.
gen se_coeff6_reg=.
gen se_coeff7_reg=.
gen se_coeff8_reg=.
gen se_coeff9_reg=.
gen se_coeff10_reg=.
gen se_coeff11_reg=.
gen se_coeff12_reg=.

gen df_reg=.
gen r2=.
gen r2_a=.

capture drop f_prix_ht 
capture drop f_prix_ht_temp
gen f_prix_ht=0


foreach i of numlist 0/29 {
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'
}

foreach i of numlist 30 {
 
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht




areg f_prix_ht c.d_dieselr_supermarche##(c.kur_demean c.freq_demean ) c.d_dieselr_nosupermarche##(c.kur_demean c.freq_demean ) ///
c.local_shock10##(c.kur_demean c.freq_demean) /// 
c.m_dprix##(c.kur_demean c.freq_demean ) ///
c.l.d_dieselr_supermarche##(c.kur_demean c.freq_demean ) ///
c.l2.d_dieselr_supermarche##(c.kur_demean c.freq_demean ) ///
c.l3.d_dieselr_supermarche##(c.kur_demean c.freq_demean ) ///
c.l4.d_dieselr_supermarche##(c.kur_demean c.freq_demean ) /// 
c.l5.d_dieselr_supermarche##(c.kur_demean c.freq_demean ) ///
c.l.d_dieselr_nosupermarche##(c.kur_demean c.freq_demean ) ///
c.l2.d_dieselr_nosupermarche##(c.kur_demean c.freq_demean ) ///
c.l3.d_dieselr_nosupermarche##(c.kur_demean c.freq_demean ) ///
c.l4.d_dieselr_nosupermarche##(c.kur_demean c.freq_demean ) /// 
c.l5.d_dieselr_nosupermarche##(c.kur_demean c.freq_demean ) ///
c.l.local_shock10##(c.kur_demean c.freq_demean )  /// 
c.l2.local_shock10##(c.kur_demean c.freq_demean ) /// 
c.l3.local_shock10##(c.kur_demean c.freq_demean ) ///
c.l4.local_shock10##(c.kur_demean c.freq_demean ) /// 
c.l5.local_shock10##(c.kur_demean c.freq_demean ) ///
c.l.m_dprix##(c.kur_demean c.freq_demean )  /// 
c.l2.m_dprix##(c.kur_demean c.freq_demean ) /// 
c.l3.m_dprix##(c.kur_demean c.freq_demean ) /// 
c.l4.m_dprix##(c.kur_demean c.freq_demean ) ///
c.l5.m_dprix##(c.kur_demean c.freq_demean ), absorb(id) vce(cluster daten)
	
replace r2=e(r2)
replace r2_a=e(r2_a)
	
matrix test=r(table)
replace coeff1_reg=_b[d_dieselr_supermarche] if num_obs==`i'
replace coeff2_reg=_b[d_dieselr_nosupermarche] if num_obs==`i'
replace coeff3_reg=_b[c.d_dieselr_supermarche#c.freq_demean] if num_obs==`i'
replace coeff4_reg=_b[c.d_dieselr_supermarche#c.kur_demean] if num_obs==`i'
replace coeff5_reg=_b[c.d_dieselr_nosupermarche#c.freq_demean] if num_obs==`i'
replace coeff6_reg=_b[c.d_dieselr_nosupermarche#c.kur_demean] if num_obs==`i'


replace coeff_c_reg=_b[_cons] if num_obs==`i'

replace se_coeff1_reg=_se[d_dieselr_supermarche] if num_obs==`i'
replace se_coeff2_reg=_se[d_dieselr_nosupermarche] if num_obs==`i'
replace se_coeff3_reg=_se[c.d_dieselr_supermarche#c.freq_demean] if num_obs==`i'
replace se_coeff4_reg=_se[c.d_dieselr_supermarche#c.kur_demean] if num_obs==`i'
replace se_coeff5_reg=_se[c.d_dieselr_nosupermarche#c.freq_demean] if num_obs==`i'
replace se_coeff6_reg=_se[c.d_dieselr_nosupermarche#c.kur_demean] if num_obs==`i'


replace df_reg=e(df_r) if num_obs==`i'
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'

preserve
keep coeff* r2 r2_a  se_coeff* df_reg num_obs
gen name="`i'"
gen charact="unconstrained"
keep if num_obs==30
save "$path\Graphs_Tables\Table5_unconstrained_PanelB_supermarkets", replace
restore 
}


******* Panel C: Urban areas



use "$path\Intermediary Data\localshock_dataforReg_v2.dta",clear


drop if prix_ht==.
drop j j3
replace d_dieselr=resid_movav_diesel
xtset id daten
gen d_dieselrott=ln(diesel_rotterdam_euro)-ln(l1.diesel_rotterdam_euro)
drop diesel_rotterdam_euro resid_movav_diesel dprix1 

drop d_dieselr
gen d_dieselr=d_dieselrott

merge m:1 id_pdv using "$path\Intermediary Data\data_PDV_autoroutes_supermarches_v3_withlonglat_mergecodeinsee.dta"
keep if _merge!=2 
drop _merge

merge m:m id using  "$path\Intermediary Data\base_id_suff_stat_6Y"
keep if _m==3
drop _m

merge m:m id using  "$path\Intermediary Data\base_base_suff_stat_6Y_forinteractkurtfreq"
keep if _m==3
drop _m


*** Merge with UU
merge m:1 codeinsee using "$path\Intermediary Data\UU2010_au_01-01-2020.dta"
keep if _merge!=2 
drop _merge



*** Rural Urbain ***
gen rural=type_com=="RURAL"
gen ville_centre=statut_2017=="C"
gen ville_banlieue=statut_2017=="B"
gen ville_isolee=statut_2017=="I"
gen ville_rurale=statut_2017=="R"



***** PLACEBO***
***** CONSTRAINED 

capture drop ratio
gen ratio=kurtosis/freq

capture drop d_dieselr_rural
capture drop d_dieselr_urbain

gen d_dieselr_rural=.
replace d_dieselr_rural=d_dieselr if rural==1
replace d_dieselr_rural=0 if rural==0

gen d_dieselr_urbain=.
replace d_dieselr_urbain=d_dieselr if rural==0
replace d_dieselr_urbain=0 if rural==1

summ d_dieselr, detail 


drop num_obs
gen num_obs=_n-1

capture drop local_shock5 local_shock15 local_shock4 local_shock6 local_shock7 local_shock8
capture drop coeff1_reg
capture drop coeff2_reg
capture drop coeff3_reg
capture drop coeff4_reg
capture drop coeff5_reg
capture drop coeff6_reg
capture drop coeff7_reg
capture drop coeff8_reg
capture drop coeff9_reg
capture drop coeff10_reg



capture drop coeff_c_reg


capture drop se_coeff1_reg
capture drop se_coeff2_reg
capture drop se_coeff3_reg
capture drop se_coeff4_reg
capture drop se_coeff5_reg
capture drop se_coeff6_reg
capture drop se_coeff7_reg
capture drop se_coeff8_reg
capture drop se_coeff9_reg
capture drop se_coeff10_reg

capture drop df_reg
capture drop r2
capture drop r2_a


gen coeff1_reg=.
gen coeff2_reg=.
gen coeff3_reg=.
gen coeff4_reg=.
gen coeff5_reg=.
gen coeff6_reg=.
gen coeff7_reg=.
gen coeff8_reg=.
gen coeff9_reg=.
gen coeff10_reg=.
gen coeff_c_reg=.


gen se_coeff1_reg=.
gen se_coeff2_reg=.
gen se_coeff3_reg=.
gen se_coeff4_reg=.
gen se_coeff5_reg=.
gen se_coeff6_reg=.
gen se_coeff7_reg=.
gen se_coeff8_reg=.
gen se_coeff9_reg=.
gen se_coeff10_reg=.
gen df_reg=.

gen r2=.
gen r2_a=.


****** H=30 ****

capture drop f_prix_ht 
capture drop f_prix_ht_temp
gen f_prix_ht=0

foreach i of numlist 1/29 {
 
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'
}


foreach i of numlist 30 {
 
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht


areg f_prix_ht c.d_dieselr_rural##(c.ratio ) c.d_dieselr_urbain##(c.ratio )  ///
c.local_shock10##(c.ratio ) /// 
c.m_dprix##(c.ratio ) ///
 c.l.d_dieselr_rural##(c.ratio ) ///
 c.l2.d_dieselr_rural##(c.ratio ) ///
 c.l3.d_dieselr_rural##(c.ratio ) ///
 c.l4.d_dieselr_rural##(c.ratio ) /// 
 c.l5.d_dieselr_rural##(c.ratio ) ///
 c.l.d_dieselr_urbain##(c.ratio ) ///
 c.l2.d_dieselr_urbain##(c.ratio ) ///
 c.l3.d_dieselr_urbain##(c.ratio ) ///
 c.l4.d_dieselr_urbain##(c.ratio ) /// 
 c.l5.d_dieselr_urbain##(c.ratio ) ///
		c.l.local_shock10##(c.ratio )  /// 
		c.l2.local_shock10##(c.ratio ) /// 
		c.l3.local_shock10##(c.ratio ) ///
		c.l4.local_shock10##(c.ratio ) /// 
		c.l5.local_shock10##(c.ratio ) ///
	c.l.m_dprix##(c.ratio )  /// 
	c.l2.m_dprix##(c.ratio ) /// 
	c.l3.m_dprix##(c.ratio ) /// 
	c.l4.m_dprix##(c.ratio ) ///
	c.l5.m_dprix##(c.ratio ), absorb(id) vce(cluster daten)

	
	replace r2=e(r2)
replace r2_a=e(r2_a)
	
matrix test=r(table)
matrix test=r(table)
replace coeff1_reg=_b[d_dieselr_rural] if num_obs==`i'
replace coeff2_reg=_b[d_dieselr_urbain] if num_obs==`i'
replace coeff3_reg=_b[c.d_dieselr_rural#c.ratio] if num_obs==`i'
replace coeff4_reg=_b[c.d_dieselr_urbain#c.ratio] if num_obs==`i'

replace coeff_c_reg=_b[_cons] if num_obs==`i'



replace se_coeff1_reg=_se[d_dieselr_rural] if num_obs==`i'
replace se_coeff2_reg=_se[d_dieselr_urbain] if num_obs==`i'
replace se_coeff3_reg=_se[c.d_dieselr_rural#c.ratio] if num_obs==`i'
replace se_coeff4_reg=_se[c.d_dieselr_urbain#c.ratio] if num_obs==`i'

replace df_reg=e(df_r) if num_obs==`i'
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'

preserve
keep coeff* r2 r2_a  se_coeff* df_reg num_obs
gen name="`i'"
gen charact="constrained"
keep if num_obs==30
save "$path\Graphs_Tables\Table5_constrained_PanelC_rurbain", replace
restore 

}

***** UNCONSTRAINED ****



bysort id: gen first_obs=1 if _n==1

egen mean_kur_temp=mean(kur) if first_obs==1
egen mean_freq_temp=mean(freq) if first_obs==1
egen mean_kur=mean(mean_kur_temp)
egen mean_freq=mean(mean_freq_temp)
drop mean_kur_temp mean_freq_temp first_obs

gen kur_demean=kur/mean_kur
gen freq_demean=freq/mean_freq


drop num_obs
gen num_obs=_n-1

capture drop local_shock5 local_shock15 local_shock4 local_shock6 local_shock7 local_shock8
capture drop coeff1_reg
capture drop coeff2_reg
capture drop coeff3_reg
capture drop coeff4_reg
capture drop coeff5_reg
capture drop coeff6_reg
capture drop coeff7_reg
capture drop coeff8_reg
capture drop coeff9_reg
capture drop coeff10_reg
capture drop coeff11_reg
capture drop coeff12_reg



capture drop coeff_c_reg

capture drop se_coeff1_reg
capture drop se_coeff2_reg
capture drop se_coeff3_reg
capture drop se_coeff4_reg
capture drop se_coeff5_reg
capture drop se_coeff6_reg
capture drop se_coeff7_reg
capture drop se_coeff8_reg
capture drop se_coeff9_reg
capture drop se_coeff10_reg
capture drop se_coeff11_reg
capture drop se_coeff12_reg

capture drop df_reg
capture drop r2
capture drop r2_a


gen coeff1_reg=.
gen coeff2_reg=.
gen coeff3_reg=.
gen coeff4_reg=.
gen coeff5_reg=.
gen coeff6_reg=.
gen coeff7_reg=.
gen coeff8_reg=.
gen coeff9_reg=.
gen coeff10_reg=.
gen coeff11_reg=.
gen coeff12_reg=.
gen coeff_c_reg=.


gen se_coeff1_reg=.
gen se_coeff2_reg=.
gen se_coeff3_reg=.
gen se_coeff4_reg=.
gen se_coeff5_reg=.
gen se_coeff6_reg=.
gen se_coeff7_reg=.
gen se_coeff8_reg=.
gen se_coeff9_reg=.
gen se_coeff10_reg=.
gen se_coeff11_reg=.
gen se_coeff12_reg=.

gen df_reg=.
gen r2=.
gen r2_a=.

capture drop f_prix_ht 
capture drop f_prix_ht_temp
gen f_prix_ht=0



**** H=30 ***


drop num_obs
gen num_obs=_n-1

capture drop local_shock5 local_shock15 local_shock4 local_shock6 local_shock7 local_shock8
capture drop coeff1_reg
capture drop coeff2_reg
capture drop coeff3_reg
capture drop coeff4_reg
capture drop coeff5_reg
capture drop coeff6_reg
capture drop coeff7_reg
capture drop coeff8_reg
capture drop coeff9_reg
capture drop coeff10_reg
capture drop coeff11_reg
capture drop coeff12_reg



capture drop coeff_c_reg


capture drop se_coeff1_reg
capture drop se_coeff2_reg
capture drop se_coeff3_reg
capture drop se_coeff4_reg
capture drop se_coeff5_reg
capture drop se_coeff6_reg
capture drop se_coeff7_reg
capture drop se_coeff8_reg
capture drop se_coeff9_reg
capture drop se_coeff10_reg
capture drop se_coeff11_reg
capture drop se_coeff12_reg

capture drop df_reg
capture drop r2
capture drop r2_a


gen coeff1_reg=.
gen coeff2_reg=.
gen coeff3_reg=.
gen coeff4_reg=.
gen coeff5_reg=.
gen coeff6_reg=.
gen coeff7_reg=.
gen coeff8_reg=.
gen coeff9_reg=.
gen coeff10_reg=.
gen coeff11_reg=.
gen coeff12_reg=.
gen coeff_c_reg=.


gen se_coeff1_reg=.
gen se_coeff2_reg=.
gen se_coeff3_reg=.
gen se_coeff4_reg=.
gen se_coeff5_reg=.
gen se_coeff6_reg=.
gen se_coeff7_reg=.
gen se_coeff8_reg=.
gen se_coeff9_reg=.
gen se_coeff10_reg=.
gen se_coeff11_reg=.
gen se_coeff12_reg=.

gen df_reg=.
gen r2=.
gen r2_a=.

capture drop f_prix_ht 
capture drop f_prix_ht_temp
gen f_prix_ht=0


foreach i of numlist 0/29 {
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'
}

foreach i of numlist 30 {
 
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht




areg f_prix_ht c.d_dieselr_rural##(c.kur_demean c.freq_demean ) c.d_dieselr_urbain##(c.kur_demean c.freq_demean ) ///
c.local_shock10##(c.kur_demean c.freq_demean) /// 
c.m_dprix##(c.kur_demean c.freq_demean ) ///
c.l.d_dieselr_rural##(c.kur_demean c.freq_demean ) ///
c.l2.d_dieselr_rural##(c.kur_demean c.freq_demean ) ///
c.l3.d_dieselr_rural##(c.kur_demean c.freq_demean ) ///
c.l4.d_dieselr_rural##(c.kur_demean c.freq_demean ) /// 
c.l5.d_dieselr_rural##(c.kur_demean c.freq_demean ) ///
c.l.d_dieselr_urbain##(c.kur_demean c.freq_demean ) ///
c.l2.d_dieselr_urbain##(c.kur_demean c.freq_demean ) ///
c.l3.d_dieselr_urbain##(c.kur_demean c.freq_demean ) ///
c.l4.d_dieselr_urbain##(c.kur_demean c.freq_demean ) /// 
c.l5.d_dieselr_urbain##(c.kur_demean c.freq_demean ) ///
c.l.local_shock10##(c.kur_demean c.freq_demean )  /// 
c.l2.local_shock10##(c.kur_demean c.freq_demean ) /// 
c.l3.local_shock10##(c.kur_demean c.freq_demean ) ///
c.l4.local_shock10##(c.kur_demean c.freq_demean ) /// 
c.l5.local_shock10##(c.kur_demean c.freq_demean ) ///
c.l.m_dprix##(c.kur_demean c.freq_demean )  /// 
c.l2.m_dprix##(c.kur_demean c.freq_demean ) /// 
c.l3.m_dprix##(c.kur_demean c.freq_demean ) /// 
c.l4.m_dprix##(c.kur_demean c.freq_demean ) ///
c.l5.m_dprix##(c.kur_demean c.freq_demean ), absorb(id) vce(cluster daten)
	
replace r2=e(r2)
replace r2_a=e(r2_a)
	
matrix test=r(table)
replace coeff1_reg=_b[d_dieselr_rural] if num_obs==`i'
replace coeff2_reg=_b[d_dieselr_urbain] if num_obs==`i'
replace coeff3_reg=_b[c.d_dieselr_rural#c.freq_demean] if num_obs==`i'
replace coeff4_reg=_b[c.d_dieselr_rural#c.kur_demean] if num_obs==`i'
replace coeff5_reg=_b[c.d_dieselr_urbain#c.freq_demean] if num_obs==`i'
replace coeff6_reg=_b[c.d_dieselr_urbain#c.kur_demean] if num_obs==`i'


replace coeff_c_reg=_b[_cons] if num_obs==`i'

replace se_coeff1_reg=_se[d_dieselr_rural] if num_obs==`i'
replace se_coeff2_reg=_se[d_dieselr_urbain] if num_obs==`i'
replace se_coeff3_reg=_se[c.d_dieselr_rural#c.freq_demean] if num_obs==`i'
replace se_coeff4_reg=_se[c.d_dieselr_rural#c.kur_demean] if num_obs==`i'
replace se_coeff5_reg=_se[c.d_dieselr_urbain#c.freq_demean] if num_obs==`i'
replace se_coeff6_reg=_se[c.d_dieselr_urbain#c.kur_demean] if num_obs==`i'



replace df_reg=e(df_r) if num_obs==`i'
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'

preserve
keep coeff* r2 r2_a  se_coeff* df_reg num_obs
gen name="`i'"
gen charact="unconstrained"
keep if num_obs==30
save "$path\Graphs_Tables\Table5_unconstrained_PanelC_rurbain", replace
restore 
}



***** Panel D : psychological prices


use "$path\Intermediary Data\localshock_dataforReg_v2.dta",clear



drop if prix_ht==.
drop  j j3
replace d_dieselr=resid_movav_diesel
xtset id daten
gen d_dieselrott=ln(diesel_rotterdam_euro)-ln(l1.diesel_rotterdam_euro)
drop diesel_rotterdam_euro resid_movav_diesel dprix1 

drop d_dieselr
gen d_dieselr=d_dieselrott

merge m:m id using  "$path\Intermediary Data\base_id_suff_stat_6Y"
keep if _m==3
drop _m

merge m:m id using  "$path\Intermediary Data\base_base_suff_stat_6Y_forinteractkurtfreq"
keep if _m==3
drop _m





gen prix_str=string(prix)
gen length_prix=length(prix_str)
capture drop last_digit
gen last_digit=substr(prix_str, length_prix,1)
destring last_digit, replace force
drop prix_str
drop length_prix


bysort id (daten): gen change_price=1 if prix[_n]!=prix[_n+1] 
capture drop psychological_prices
capture drop sum_psychological_prices
capture drop sum_change_price
capture drop mean_psychological_prices
 
gen psychological_prices=0 if change_price==1
replace psychological_prices=1 if change_price==1 & (last_digit==0 | last_digit==9)
bysort id (daten): egen sum_psychological_prices=sum(psychological_prices)
bysort id (daten): egen sum_change_price=sum(change_price)
gen mean_psychological_prices=sum_psychological_prices/sum_change_price


capture drop psycho
sum mean_psychological_prices,d
gen psycho=(mean_psychological_prices>.4268617) 
replace psycho=. if mean_psychological_prices==.


***** PLACEBO***
***** CONSTRAINED 

capture drop ratio
gen ratio=kurtosis/freq

capture drop d_dieselr_psychohigh
capture drop d_dieselr_psycholow

gen d_dieselr_psychohigh=.
replace d_dieselr_psychohigh=d_dieselr if psycho==1
replace d_dieselr_psychohigh=0 if psycho==0

gen d_dieselr_psycholow=.
replace d_dieselr_psycholow=d_dieselr if psycho==0
replace d_dieselr_psycholow=0 if psycho==1

summ d_dieselr, detail 


drop num_obs
gen num_obs=_n-1

capture drop local_shock5 local_shock15 local_shock4 local_shock6 local_shock7 local_shock8
capture drop coeff1_reg
capture drop coeff2_reg
capture drop coeff3_reg
capture drop coeff4_reg
capture drop coeff5_reg
capture drop coeff6_reg
capture drop coeff7_reg
capture drop coeff8_reg
capture drop coeff9_reg
capture drop coeff10_reg



capture drop coeff_c_reg


capture drop se_coeff1_reg
capture drop se_coeff2_reg
capture drop se_coeff3_reg
capture drop se_coeff4_reg
capture drop se_coeff5_reg
capture drop se_coeff6_reg
capture drop se_coeff7_reg
capture drop se_coeff8_reg
capture drop se_coeff9_reg
capture drop se_coeff10_reg

capture drop df_reg
capture drop r2
capture drop r2_a


gen coeff1_reg=.
gen coeff2_reg=.
gen coeff3_reg=.
gen coeff4_reg=.
gen coeff5_reg=.
gen coeff6_reg=.
gen coeff7_reg=.
gen coeff8_reg=.
gen coeff9_reg=.
gen coeff10_reg=.
gen coeff_c_reg=.


gen se_coeff1_reg=.
gen se_coeff2_reg=.
gen se_coeff3_reg=.
gen se_coeff4_reg=.
gen se_coeff5_reg=.
gen se_coeff6_reg=.
gen se_coeff7_reg=.
gen se_coeff8_reg=.
gen se_coeff9_reg=.
gen se_coeff10_reg=.
gen df_reg=.

gen r2=.
gen r2_a=.


****** H=30 ****

capture drop f_prix_ht 
capture drop f_prix_ht_temp
gen f_prix_ht=0

foreach i of numlist 1/29 {
 
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'
}


foreach i of numlist 30 {
 
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht




areg f_prix_ht c.d_dieselr_psychohigh##(c.ratio ) c.d_dieselr_psycholow##(c.ratio )  ///
c.local_shock10##(c.ratio ) /// 
c.m_dprix##(c.ratio ) ///
 c.l.d_dieselr_psychohigh##(c.ratio ) ///
 c.l2.d_dieselr_psychohigh##(c.ratio ) ///
 c.l3.d_dieselr_psychohigh##(c.ratio ) ///
 c.l4.d_dieselr_psychohigh##(c.ratio ) /// 
 c.l5.d_dieselr_psychohigh##(c.ratio ) ///
 c.l.d_dieselr_psycholow##(c.ratio ) ///
 c.l2.d_dieselr_psycholow##(c.ratio ) ///
 c.l3.d_dieselr_psycholow##(c.ratio ) ///
 c.l4.d_dieselr_psycholow##(c.ratio ) /// 
 c.l5.d_dieselr_psycholow##(c.ratio ) ///
		c.l.local_shock10##(c.ratio )  /// 
		c.l2.local_shock10##(c.ratio ) /// 
		c.l3.local_shock10##(c.ratio ) ///
		c.l4.local_shock10##(c.ratio ) /// 
		c.l5.local_shock10##(c.ratio ) ///
	c.l.m_dprix##(c.ratio )  /// 
	c.l2.m_dprix##(c.ratio ) /// 
	c.l3.m_dprix##(c.ratio ) /// 
	c.l4.m_dprix##(c.ratio ) ///
	c.l5.m_dprix##(c.ratio ), absorb(id) vce(cluster daten)

	
	replace r2=e(r2)
replace r2_a=e(r2_a)
	
matrix test=r(table)
matrix test=r(table)
replace coeff1_reg=_b[d_dieselr_psychohigh] if num_obs==`i'
replace coeff2_reg=_b[d_dieselr_psycholow] if num_obs==`i'
replace coeff3_reg=_b[c.d_dieselr_psychohigh#c.ratio] if num_obs==`i'
replace coeff4_reg=_b[c.d_dieselr_psycholow#c.ratio] if num_obs==`i'

replace coeff_c_reg=_b[_cons] if num_obs==`i'



replace se_coeff1_reg=_se[d_dieselr_psychohigh] if num_obs==`i'
replace se_coeff2_reg=_se[d_dieselr_psycholow] if num_obs==`i'
replace se_coeff3_reg=_se[c.d_dieselr_psychohigh#c.ratio] if num_obs==`i'
replace se_coeff4_reg=_se[c.d_dieselr_psycholow#c.ratio] if num_obs==`i'


replace df_reg=e(df_r) if num_obs==`i'
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'

preserve
keep coeff* r2 r2_a  se_coeff* df_reg num_obs
gen name="`i'"
gen charact="constrained"
keep if num_obs==30
save "$path\Graphs_Tables\Table5_constrained_PanelD_psycho", replace
restore 
}


***** UNCONSTRAINED ****



bysort id: gen first_obs=1 if _n==1

egen mean_kur_temp=mean(kur) if first_obs==1
egen mean_freq_temp=mean(freq) if first_obs==1
egen mean_kur=mean(mean_kur_temp)
egen mean_freq=mean(mean_freq_temp)
drop mean_kur_temp mean_freq_temp first_obs

gen kur_demean=kur/mean_kur
gen freq_demean=freq/mean_freq


drop num_obs
gen num_obs=_n-1

capture drop local_shock5 local_shock15 local_shock4 local_shock6 local_shock7 local_shock8
capture drop coeff1_reg
capture drop coeff2_reg
capture drop coeff3_reg
capture drop coeff4_reg
capture drop coeff5_reg
capture drop coeff6_reg
capture drop coeff7_reg
capture drop coeff8_reg
capture drop coeff9_reg
capture drop coeff10_reg
capture drop coeff11_reg
capture drop coeff12_reg



capture drop coeff_c_reg


capture drop se_coeff1_reg
capture drop se_coeff2_reg
capture drop se_coeff3_reg
capture drop se_coeff4_reg
capture drop se_coeff5_reg
capture drop se_coeff6_reg
capture drop se_coeff7_reg
capture drop se_coeff8_reg
capture drop se_coeff9_reg
capture drop se_coeff10_reg
capture drop se_coeff11_reg
capture drop se_coeff12_reg

capture drop df_reg
capture drop r2
capture drop r2_a


gen coeff1_reg=.
gen coeff2_reg=.
gen coeff3_reg=.
gen coeff4_reg=.
gen coeff5_reg=.
gen coeff6_reg=.
gen coeff7_reg=.
gen coeff8_reg=.
gen coeff9_reg=.
gen coeff10_reg=.
gen coeff11_reg=.
gen coeff12_reg=.
gen coeff_c_reg=.


gen se_coeff1_reg=.
gen se_coeff2_reg=.
gen se_coeff3_reg=.
gen se_coeff4_reg=.
gen se_coeff5_reg=.
gen se_coeff6_reg=.
gen se_coeff7_reg=.
gen se_coeff8_reg=.
gen se_coeff9_reg=.
gen se_coeff10_reg=.
gen se_coeff11_reg=.
gen se_coeff12_reg=.

gen df_reg=.
gen r2=.
gen r2_a=.

capture drop f_prix_ht 
capture drop f_prix_ht_temp
gen f_prix_ht=0



**** H=30 ***


drop num_obs
gen num_obs=_n-1

capture drop local_shock5 local_shock15 local_shock4 local_shock6 local_shock7 local_shock8
capture drop coeff1_reg
capture drop coeff2_reg
capture drop coeff3_reg
capture drop coeff4_reg
capture drop coeff5_reg
capture drop coeff6_reg
capture drop coeff7_reg
capture drop coeff8_reg
capture drop coeff9_reg
capture drop coeff10_reg
capture drop coeff11_reg
capture drop coeff12_reg



capture drop coeff_c_reg


capture drop se_coeff1_reg
capture drop se_coeff2_reg
capture drop se_coeff3_reg
capture drop se_coeff4_reg
capture drop se_coeff5_reg
capture drop se_coeff6_reg
capture drop se_coeff7_reg
capture drop se_coeff8_reg
capture drop se_coeff9_reg
capture drop se_coeff10_reg
capture drop se_coeff11_reg
capture drop se_coeff12_reg

capture drop df_reg
capture drop r2
capture drop r2_a


gen coeff1_reg=.
gen coeff2_reg=.
gen coeff3_reg=.
gen coeff4_reg=.
gen coeff5_reg=.
gen coeff6_reg=.
gen coeff7_reg=.
gen coeff8_reg=.
gen coeff9_reg=.
gen coeff10_reg=.
gen coeff11_reg=.
gen coeff12_reg=.
gen coeff_c_reg=.


gen se_coeff1_reg=.
gen se_coeff2_reg=.
gen se_coeff3_reg=.
gen se_coeff4_reg=.
gen se_coeff5_reg=.
gen se_coeff6_reg=.
gen se_coeff7_reg=.
gen se_coeff8_reg=.
gen se_coeff9_reg=.
gen se_coeff10_reg=.
gen se_coeff11_reg=.
gen se_coeff12_reg=.

gen df_reg=.
gen r2=.
gen r2_a=.

capture drop f_prix_ht 
capture drop f_prix_ht_temp
gen f_prix_ht=0


foreach i of numlist 0/29 {
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'
}

foreach i of numlist 30 {
 
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht



areg f_prix_ht c.d_dieselr_psychohigh##(c.kur_demean c.freq_demean ) c.d_dieselr_psycholow##(c.kur_demean c.freq_demean ) ///
c.local_shock10##(c.kur_demean c.freq_demean) /// 
c.m_dprix##(c.kur_demean c.freq_demean ) ///
c.l.d_dieselr_psychohigh##(c.kur_demean c.freq_demean ) ///
c.l2.d_dieselr_psychohigh##(c.kur_demean c.freq_demean ) ///
c.l3.d_dieselr_psychohigh##(c.kur_demean c.freq_demean ) ///
c.l4.d_dieselr_psychohigh##(c.kur_demean c.freq_demean ) /// 
c.l5.d_dieselr_psychohigh##(c.kur_demean c.freq_demean ) ///
c.l.d_dieselr_psycholow##(c.kur_demean c.freq_demean ) ///
c.l2.d_dieselr_psycholow##(c.kur_demean c.freq_demean ) ///
c.l3.d_dieselr_psycholow##(c.kur_demean c.freq_demean ) ///
c.l4.d_dieselr_psycholow##(c.kur_demean c.freq_demean ) /// 
c.l5.d_dieselr_psycholow##(c.kur_demean c.freq_demean ) ///
c.l.local_shock10##(c.kur_demean c.freq_demean )  /// 
c.l2.local_shock10##(c.kur_demean c.freq_demean ) /// 
c.l3.local_shock10##(c.kur_demean c.freq_demean ) ///
c.l4.local_shock10##(c.kur_demean c.freq_demean ) /// 
c.l5.local_shock10##(c.kur_demean c.freq_demean ) ///
c.l.m_dprix##(c.kur_demean c.freq_demean )  /// 
c.l2.m_dprix##(c.kur_demean c.freq_demean ) /// 
c.l3.m_dprix##(c.kur_demean c.freq_demean ) /// 
c.l4.m_dprix##(c.kur_demean c.freq_demean ) ///
c.l5.m_dprix##(c.kur_demean c.freq_demean ), absorb(id) vce(cluster daten)
	
replace r2=e(r2)
replace r2_a=e(r2_a)
	
matrix test=r(table)
replace coeff1_reg=_b[d_dieselr_psychohigh] if num_obs==`i'
replace coeff2_reg=_b[d_dieselr_psycholow] if num_obs==`i'
replace coeff3_reg=_b[c.d_dieselr_psychohigh#c.freq_demean] if num_obs==`i'
replace coeff4_reg=_b[c.d_dieselr_psychohigh#c.kur_demean] if num_obs==`i'
replace coeff5_reg=_b[c.d_dieselr_psycholow#c.freq_demean] if num_obs==`i'
replace coeff6_reg=_b[c.d_dieselr_psycholow#c.kur_demean] if num_obs==`i'


replace coeff_c_reg=_b[_cons] if num_obs==`i'

replace se_coeff1_reg=_se[d_dieselr_psychohigh] if num_obs==`i'
replace se_coeff2_reg=_se[d_dieselr_psycholow] if num_obs==`i'
replace se_coeff3_reg=_se[c.d_dieselr_psychohigh#c.freq_demean] if num_obs==`i'
replace se_coeff4_reg=_se[c.d_dieselr_psychohigh#c.kur_demean] if num_obs==`i'
replace se_coeff5_reg=_se[c.d_dieselr_psycholow#c.freq_demean] if num_obs==`i'
replace se_coeff6_reg=_se[c.d_dieselr_psycholow#c.kur_demean] if num_obs==`i'





replace df_reg=e(df_r) if num_obs==`i'
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'

preserve
keep coeff* r2 r2_a  se_coeff* df_reg num_obs
gen name="`i'"
gen charact="unconstrained"
keep if num_obs==30
save "$path\Graphs_Tables\Table5_unconstrained_PanelD_psycho", replace
restore 
}




*** Append all results

clear
use "$path\Graphs_Tables\Table5_constrained_PanelA_pricelev"
append using "$path\Graphs_Tables\Table5_constrained_PanelB_supermarkets"
append using "$path\Graphs_Tables\Table5_constrained_PanelC_rurbain"
append using "$path\Graphs_Tables\Table5_constrained_PanelD_psycho"


gen id_col=_n

xpose, clear

drop if _n==1

set obs 40

capture drop col1
capture drop col2
capture drop col3



gen col1=.
replace col1=v1[3] if _n==1
replace col1=v1[14] if _n==2
replace col1=v1[4] if _n==3
replace col1=v1[15] if _n==4

replace col1=v2[3] if _n==5
replace col1=v2[14] if _n==6
replace col1=v2[4] if _n==7
replace col1=v2[15] if _n==8

replace col1=v3[3] if _n==9
replace col1=v3[14] if _n==10
replace col1=v3[4] if _n==11
replace col1=v3[15] if _n==12

replace col1=v4[4] if _n==13
replace col1=v4[15] if _n==14
replace col1=v4[3] if _n==15
replace col1=v4[14] if _n==16




gen col2=.
replace col2=v1[1] if _n==1
replace col2=v1[12] if _n==2
replace col2=v1[2] if _n==3
replace col2=v1[13] if _n==4

replace col2=v2[1] if _n==5
replace col2=v2[12] if _n==6
replace col2=v2[2] if _n==7
replace col2=v2[13] if _n==8

replace col2=v3[1] if _n==9
replace col2=v3[12] if _n==10
replace col2=v3[2] if _n==11
replace col2=v3[13] if _n==12

replace col2=v4[2] if _n==13
replace col2=v4[13] if _n==14
replace col2=v4[1] if _n==15
replace col2=v4[12] if _n==16




gen col3=.
replace col3=v1[23] if _n==1
replace col3=v2[23] if _n==5
replace col3=v3[23] if _n==9
replace col3=v4[23] if _n==13


gen Panel="Panel A: Price levels" if _n==1
replace Panel="Panel B: Supermarkets" if _n==5
replace Panel="Panel C: Urban areas" if _n==9
replace Panel="Panel D: Psychological prices" if _n==13


gen Horizon="H=30 days" if _n==1
replace Horizon="H=30 days" if _n==5
replace Horizon="H=30 days" if _n==9
replace Horizon="H=30 days" if _n==13


gen Var="Below median" if _n==1
replace Var="Above median" if _n==3
replace Var="Supermarkets" if _n==5
replace Var="Non-supermarkets" if _n==7
replace Var="Rural" if _n==9
replace Var="Urban" if _n==11
replace Var="Below median" if _n==13
replace Var="Above median" if _n==15



drop v*

gen tstatcol1=.
gen tstatcol2=.



foreach i in 1 3 5 7 9 11 13 15{
replace tstatcol1=col1[`i']/col1[`i'+1] if _n==`i'
replace tstatcol2=col2[`i']/col2[`i'+1] if _n==`i'
}

gen starscol1=""
gen starscol2=""


foreach i in 1 2 {
replace starscol`i'="***" if abs(tstatcol`i')>=2.58 & tstatcol`i'!=.
replace starscol`i'="**" if abs(tstatcol`i')<2.58 & abs(tstatcol`i')>=1.96 & tstatcol`i'!=.
replace starscol`i'="*" if abs(tstatcol`i')<1.96 & abs(tstatcol`i')>=1.64 & tstatcol`i'!=.
}

drop tstat*
rename col* coltemp*

foreach i in 1 2 {
replace coltemp`i'=round(coltemp`i', 0.001)
tostring coltemp`i', gen(coltempbis`i') force format("%9.3f")  
egen col`i'=concat(coltempbis`i' starscol`i')
}


rename col1 ShockxRatio
rename col2 Shock_constr
rename coltemp3 R2_constr


keep Panel Var ShockxRatio Shock_constr R2_constr 
order Panel Var ShockxRatio Shock_constr R2_constr 
replace R2=round(R2, 0.001)
format %9.3f  R2
drop if _n>=17


gen num_obs=_n

save "$path\Graphs_Tables\Table5_constrained_edited.dta", replace






*** Append all results

clear
use "$path\Graphs_Tables\Table5_unconstrained_PanelA_pricelev"
append using "$path\Graphs_Tables\Table5_unconstrained_PanelB_supermarkets"
append using "$path\Graphs_Tables\Table5_unconstrained_PanelC_rurbain"
append using "$path\Graphs_Tables\Table5_unconstrained_PanelD_psycho"

gen id_col=_n

xpose, clear

drop if _n==1

set obs 40

capture drop col1
capture drop col2
capture drop col3
capture drop col4


gen col1=.
replace col1=v1[3] if _n==1
replace col1=v1[16] if _n==2
replace col1=v1[5] if _n==3
replace col1=v1[18] if _n==4

replace col1=v2[3] if _n==5
replace col1=v2[16] if _n==6
replace col1=v2[5] if _n==7
replace col1=v2[18] if _n==8

replace col1=v3[3] if _n==9
replace col1=v3[16] if _n==10
replace col1=v3[5] if _n==11
replace col1=v3[18] if _n==12

replace col1=v4[5] if _n==13
replace col1=v4[18] if _n==14
replace col1=v4[3] if _n==15
replace col1=v4[16] if _n==16



gen col2=.
replace col2=v1[4] if _n==1
replace col2=v1[17] if _n==2
replace col2=v1[6] if _n==3
replace col2=v1[19] if _n==4

replace col2=v2[4] if _n==5
replace col2=v2[17] if _n==6
replace col2=v2[6] if _n==7
replace col2=v2[19] if _n==8

replace col2=v3[4] if _n==9
replace col2=v3[17] if _n==10
replace col2=v3[6] if _n==11
replace col2=v3[19] if _n==12

replace col2=v4[6] if _n==13
replace col2=v4[19] if _n==14
replace col2=v4[4] if _n==15
replace col2=v4[17] if _n==16


gen col3=.
replace col3=v1[1] if _n==1
replace col3=v1[14] if _n==2
replace col3=v1[2] if _n==3
replace col3=v1[15] if _n==4

replace col3=v2[1] if _n==5
replace col3=v2[14] if _n==6
replace col3=v2[2] if _n==7
replace col3=v2[15] if _n==8

replace col3=v3[1] if _n==9
replace col3=v3[14] if _n==10
replace col3=v3[2] if _n==11
replace col3=v3[15] if _n==12

replace col3=v4[2] if _n==13
replace col3=v4[15] if _n==14
replace col3=v4[1] if _n==15
replace col3=v4[14] if _n==16



gen col4=.
replace col4=v1[27] if _n==1
replace col4=v2[27] if _n==5
replace col4=v3[27] if _n==9
replace col4=v4[27] if _n==13


gen Panel="Panel A: Price levels" if _n==1
replace Panel="Panel B: Supermarkets" if _n==5
replace Panel="Panel C: Urban areas" if _n==9
replace Panel="Panel D: Psychological prices" if _n==13


gen Horizon="H=30 days" if _n==1
replace Horizon="H=30 days" if _n==5
replace Horizon="H=30 days" if _n==9
replace Horizon="H=30 days" if _n==13


gen Var="Below median" if _n==1
replace Var="Above median" if _n==3
replace Var="Supermarkets" if _n==5
replace Var="Non-supermarkets" if _n==7
replace Var="Rural" if _n==9
replace Var="Urban" if _n==11
replace Var="Below median" if _n==13
replace Var="Above median" if _n==15


gen tstatcol1=.
gen tstatcol2=.
gen tstatcol3=.



foreach i in 1 3 5 7 9 11 13 15 {
replace tstatcol1=col1[`i']/col1[`i'+1] if _n==`i'
replace tstatcol2=col2[`i']/col2[`i'+1] if _n==`i'
replace tstatcol3=col3[`i']/col3[`i'+1] if _n==`i'
}

gen starscol1=""
gen starscol2=""
gen starscol3=""


foreach i in 1 2 3 {
replace starscol`i'="***" if abs(tstatcol`i')>=2.58 & tstatcol`i'!=.
replace starscol`i'="**" if abs(tstatcol`i')<2.58 & abs(tstatcol`i')>=1.96 & tstatcol`i'!=.
replace starscol`i'="*" if abs(tstatcol`i')<1.96 & abs(tstatcol`i')>=1.64 & tstatcol`i'!=.
}

drop tstat*
rename col* coltemp*

foreach i in 1 2 3 {
replace coltemp`i'=round(coltemp`i', 0.001)
tostring coltemp`i', gen(coltempbis`i') force format("%9.3f")  
egen col`i'=concat(coltempbis`i' starscol`i')
}


rename col1 ShockxFreq
rename col2 ShockxKurtosis
rename col3 Shock_uconstr
rename coltemp4 R2_uconstr


keep Panel Var ShockxFreq ShockxKurtosis Shock_uconstr R2_uconstr
order Panel Var ShockxFreq ShockxKurtosis Shock_uconstr R2_uconstr
replace R2_uconstr=round(R2_uconstr, 0.001)
format %9.3f  R2_uconstr
drop if _n>=17


gen num_obs=_n


order Panel Var ShockxFreq ShockxKurtosis Shock_uconstr R2_uconstr num_obs
save "$path\Graphs_Tables\Table5_unconstrained_edited.dta", replace

clear
use "$path\Graphs_Tables\Table5_constrained_edited.dta"
merge 1:1 num_obs using "$path\Graphs_Tables\Table5_unconstrained_edited.dta"
drop num_obs 
drop _merge

order Panel Var ShockxRatio Shock_constr R2_constr ShockxFreq ShockxKurtosis Shock_uconstr R2_uconstr

drop if _n>=17

save "$path\Graphs_Tables\Table5_ALL_edited.dta", replace




